#include "HAL.h"
#include "LPC11xx.h"			/* LPC11xx Peripheral Registers */
#include "../Common/type.h"
#include "../HAL/i2c.h"
#include "../GPIO/gpio.h"

extern uint32_t SystemFrequency ;

void HAL_I2C_Enable(UINT8 address)
{
		I2CSlaveInit(address);			/* initialize I2c */
}

void HAL_I2C_Disable()
{
}

void HAL_I2C_Write(UINT8 v)
{
	DrvI2C_WriteData(v);
}

UINT8 HAL_I2C_Read()
{
	return DrvI2C_ReadData();
	//return LPC_I2C->DAT;
}

void HAL_I2C_SetAddress(UINT8 address)
{
	if (address==0) DrvI2C_SetAddress(0, (address<<1), 0);
	else 
	DrvI2C_SetAddress(0, (address<<1)+1, 0);
	//LPC_I2C->ADR0 = address;
}
volatile unsigned int ticks;
//void HAL_Init_SysTick100us()
//{
//    ticks = 0;
//    
//    SysTick->LOAD = 100 * CyclesPerUs;
//    SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_TICKINT_Msk;
//}
void SysTick_Handler(void)
{
    ticks++;
}
void HAL_Init_SysTick100us()
{
		UINT32 freq = SystemFrequency * 4; // Clock frequency = ClkIn * M (MAIN_PLL_M_Val + 1)
	  UINT32 sysTickLoad = freq / 10000; // 10000 = 100us (1s = 1, 1ms = 1000, 1us = 1000000)
	
    ticks = 0;
    
    SystemInit();	// Initialize clocks
    
    SysTick_Config(sysTickLoad);    // Configure the SYSTICK
}
unsigned int HAL_GetTicks100us()
{
    return ticks;
}
void HAL_Delay100us(UINT32 d)
{
    unsigned int start = ticks;
    while( (ticks - start) <= d);
}
void HAL_ResetChip()
{
    //DrvSYS_ResetChip();
	NVIC_SystemReset();
}
void HAL_Assert()
{
	volatile int cont = 0;

	while(!cont);
		
}
